package com.changjiang.myproductsale.myproduct.dao.impl;

import com.changjiang.myproductsale.myproduct.dao.BaseDao;
import com.changjiang.myproductsale.myproduct.dao.ProductDao;
import com.changjiang.myproductsale.myproduct.entity.Product;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl extends BaseDao implements ProductDao {
    @Override
    public List<Product> getProductByCatePage(int category_id, int pageSize, int pageNum) {
        String sql = "select * from product  \n" +
                " inner join category on product.category_id=category.category_id\n" +
                " inner join shop on  product.shop_id=shop.shop_id\n" +
                " where product.category_id=? limit ?,?";
        try {
            List<Product> productList = new ArrayList<Product>();
            ResultSet set = executeQuery(sql, category_id, (pageNum - 1) * pageSize, pageSize);
            while (set.next()) {
                //每循环一次 读取一行商品
                Product p = new Product(
                        //...一堆列
                        set.getInt("product_id"),
                        set.getInt("category_id"),
                        set.getInt("shop_id"),
                        set.getInt("product_comment_num"),
                        set.getInt("product_hot"),
                        set.getInt("product_special"),
                        set.getInt("product_search_hot"),
                        set.getDouble("product_price"),
                        set.getDouble("product_uprice"),
                        set.getString("product_name"),
                        set.getString("product_img_url"),
                        set.getString("product_num"),
                        set.getString("product_detail"),
                        set.getString("shop_name"),
                        set.getString("category_name")
                );
                productList.add(p);
            }
            set.close();
            connection.close();
            return productList;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getProductCountByCate(int category_id) {
        String sql = "select count(*) from product where category_id=? ";
        try {
            ResultSet set = executeQuery(sql, category_id);
            set.next();
            int total = set.getInt(1);
            set.close();
            connection.close();
            return total;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public boolean save(Product product) {
        return false;
    }

    @Override
    public boolean delete(int id) {
        return false;
    }

    @Override
    public boolean update(Product product) {
        return false;
    }

    @Override
    public Product queryById(int id) {
        String sql = "select * from product  \n" +
                " inner join category on product.category_id=category.category_id\n" +
                " inner join shop on  product.shop_id=shop.shop_id\n" +
                " where product.product_id=?";
        try {
            ResultSet set = executeQuery(sql, id);
            set.next();
            //读取一个对象
            Product p = new Product(
                    //...一堆列
                    set.getInt("product_id"),
                    set.getInt("category_id"),
                    set.getInt("shop_id"),
                    set.getInt("product_comment_num"),
                    set.getInt("product_hot"),
                    set.getInt("product_special"),
                    set.getInt("product_search_hot"),
                    set.getDouble("product_price"),
                    set.getDouble("product_uprice"),
                    set.getString("product_name"),
                    set.getString("product_img_url"),
                    set.getString("product_num"),
                    set.getString("product_detail"),
                    set.getString("shop_name"),
                    set.getString("category_name")
            );
            set.close();
            connection.close();
            return p;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Product> queryAll() {
        return null;
    }

    @Override
    public List<Product> getTopSearchProducts() {
        String sql = "SELECT * FROM product ORDER BY product_search_hot DESC LIMIT 16";
        List<Product> topProducts = new ArrayList<>();
        try (Connection conn = initConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet resultSet = pstmt.executeQuery()) {
            while (resultSet.next()) {
                Product product = new Product(
                        resultSet.getInt("product_id"),
                        resultSet.getInt("product_search_hot"),
                        resultSet.getString("product_name"),
                        resultSet.getString("product_img_url") // 确保获取图片URL
                );
                topProducts.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return topProducts;
    }

    @Override
    public List<Product> getHotProducts() {
        // SQL查询语句，根据product_comment_num字段降序排列，并限制结果为前10条记录
        String sql = "SELECT product_id, product_name, product_comment_num FROM product ORDER BY product_comment_num DESC LIMIT 10";
        List<Product> hotProducts = new ArrayList<>();
        try (Connection conn = initConnection(); // 假设initConnection()方法用于初始化数据库连接
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet resultSet = pstmt.executeQuery()) {

            while (resultSet.next()) {
                // 根据resultSet中的数据创建Product对象，不包括图片URL
                Product product = new Product(
                        resultSet.getInt("product_id"),
                        resultSet.getString("product_name"),
                        resultSet.getInt("product_comment_num") // 假设Product类有product_comment_num字段
                );
                hotProducts.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return hotProducts;
    }
    }


